package com.wyth.lottery.code;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;

import java.util.HashMap;

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public class UsbDeviceList<V> extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setResult(Activity.RESULT_CANCELED);
        doDiscovery();
    }

    private void doDiscovery() {
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> devices = manager.getDeviceList();
        for (UsbDevice device : devices.values()) {
            if (device.getVendorId() == 1155) {
                returnToPreviousActivity(device);
            }
        }
    }

    private void returnToPreviousActivity(UsbDevice device) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable(UsbManager.EXTRA_DEVICE, device);
        intent.putExtras(bundle);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}
